package com.xy.day4.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.day4.biz.AnimalBiz;
import com.xy.day4.biz.TypeBiz;
import com.xy.day4.entity.Animal;

@Controller
@RequestMapping("/animal")
public class AnimalController {
	@Resource
	private AnimalBiz biz;
	@Resource
	private TypeBiz tpbiz;
	@RequestMapping("/list")
	public void list(Map<String, Object> req) {
		List<Animal> list =biz.findAll();
		System.out.println("========aaaa");
		for (Animal animal : list) {
			System.out.println(animal);
		}
		req.put("list", list);
	}
	@RequestMapping("/add")
	public void preAdd(Map<String,Object> req) {
		req.put("tplist", tpbiz.findAll());
	}
	@RequestMapping("/doadd")
	public String save(Animal an) {
		System.out.println("=========="+an);
		biz.save(an);
		return "redirect:list";
	}
	@RequestMapping("/delete")
	public String del(int animalId){
		//System.out.println("=========="+animalId);
		biz.delete(animalId);
		return "redirect:list";
	}
	@RequestMapping("update")
	public String preUpdate(Map<String, Object> req,int animalId) {
		System.out.println(animalId+"=========");
		req.put("animal", biz.findById(animalId));
		req.put("tplist", tpbiz.findAll());
		return "animal/update";
	}
	
	@RequestMapping("/doupdate")
	public String doUpdate(Animal animal){
		System.out.println("=========="+animal.getType().getTypeId());
		System.out.println("=========="+animal.getAnimalName());
		biz.update(animal);
		return "redirect:list";
}
}